﻿using System;
using System.Diagnostics;
using TimeSnapperPluginAPI;

namespace SamplePlugins
{
    public class OpeningHourPlugin : ITimeSnapperPlugin
    {
        private IServices m_services;
        private int m_openFrom;
        private int m_openTo;        
        private bool m_closed = false;

        // Use a Windows.Forms timer to keep the event in the GUI thread 
        // in order to avoid cross thread operation when starting/stopping the monitoring
        private System.Windows.Forms.Timer m_timer;

        private const string cOpenFrom = "OpeningHoursPlugin.OpenFrom";
        private const string cOpenTo = "OpeningHoursPlugin.OpenTo";

        bool ITimeSnapperPlugin.Configurable
        {
            get { return true; }
        }

        void ITimeSnapperPlugin.Configure()
        {
            OpeningHoursConfigurationForm frm = new OpeningHoursConfigurationForm();
            frm.OpenFrom = m_openFrom;
            frm.OpenTo = m_openTo;
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                m_openFrom = frm.OpenFrom;
                m_openTo = frm.OpenTo;

                // persist the settings to the database
                m_services.SettingsServices.SetPropertyValue(cOpenFrom, m_openFrom.ToString());
                m_services.SettingsServices.SetPropertyValue(cOpenTo, m_openTo.ToString());
                StartTimer();
            }
        }

        string ITimeSnapperPlugin.Description
        {
            get { return "Defines the hours that TimeSnapper takes screenshots."; }
        }

        string ITimeSnapperPlugin.FriendlyName
        {
            get { return "Opening Hours"; }
        }

        object ITimeSnapperPlugin.HandleEvent(TimeSnapperEvent timeSnapperEvent, IServices services, EventArgs args)
        {
            switch (timeSnapperEvent)
            {
                case TimeSnapperEvent.PluginsLoaded:
                    // save a reference to the services object
                    m_services = services;
                    m_openFrom = int.Parse(services.SettingsServices.GetPropertyValue(cOpenFrom, (8 * 60).ToString()));
                    m_openTo = int.Parse(services.SettingsServices.GetPropertyValue(cOpenTo, (18 * 60).ToString()));
                    StartTimer();
                    break;

                default:
                    return null;
            }
            return null;
        }

        private void StartTimer()
        {
            if (m_openFrom == m_openTo)
            {
                // the plugin hasn't been properly configured, so it's open 24x7
                return;
            }

            if (m_timer == null)
            {
                m_timer = new System.Windows.Forms.Timer();
                m_timer.Interval = 30000;
                m_timer.Tick += new EventHandler(timer_Elapsed);
            }
            m_timer.Start();
        }

        void timer_Elapsed(object sender, System.EventArgs e)
        {
            if (m_openFrom == m_openTo)
            {
                // the plugin hasn't been properly configured, so it's open 24x7
                m_closed = false;
                return;
            }

            DateTime now = DateTime.Now;
            int nowInt = now.Hour * 60 + now.Minute;

            bool outsideOpeningHours = false;

            if (m_openFrom <= m_openTo)
            {
                if (nowInt < m_openFrom || m_openTo < nowInt)
                    outsideOpeningHours = true;
            }
            else
            {
                // non-standard opening hours, e.g. between 20:00 to 06:00  (i.e. 8pm to 6am)
                if (m_openFrom < nowInt || nowInt < m_openTo)
                    outsideOpeningHours = true;
            }

            if (outsideOpeningHours)
            {
                if (!m_closed)
                {
                    // it's outside opening hours, but we still haven't closed - so lets close it down!
                    // The user can still start TimeSnapper manually during closing hours
                    m_services.Log("OpeningHoursPlugin: Closing down for today");
                    m_closed = true;
                    m_services.StopMonitoring();
                }
            }
            else
            {
                if (m_closed)
                {
                    // it's within opening hours, but we still haven't opened - so lets open up!
                    // The user can still stop TimeSnapper manually during opening hours
                    m_closed = false;
                    m_services.Log("OpeningHoursPlugin: We're open for business");
                    m_services.StartMonitoring();
                }
            }
        }

        TimeSnapperMenuItem[] ITimeSnapperPlugin.MenuItems()
        {
            return null;
        }

        Guid ITimeSnapperPlugin.PluginID
        {
            get { return new Guid("AC5317F1-59B2-44EF-BD66-6C2B91836EA5"); }
        }

        TimeSnapperEvent[] ITimeSnapperPlugin.SubscribesTo()
        {
            return new TimeSnapperEvent[] { TimeSnapperEvent.PluginsLoaded };
        }
    }
}
